﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mesuro.GUI.Controls {

	public class ExpandableTabControl : TabControl {

		#region DP: IsExpanded
		public static readonly DependencyProperty IsExpandedProperty = Expander.IsExpandedProperty.AddOwner( typeof( ExpandableTabControl ), new FrameworkPropertyMetadata( true, FrameworkPropertyMetadataOptions.None, IsExpandedChangedStatic ) );
		public bool IsExpanded {
			get { return (bool)GetValue( IsExpandedProperty ); }
			set { SetValue( IsExpandedProperty, value ); }
		}
		public static bool GetIsExpanded( DependencyObject element ) { return (bool)element.GetValue( IsExpandedProperty ); }
		public static void SetIsExpanded( DependencyObject element, bool value ) { element.SetValue( IsExpandedProperty, value ); }
		private static void IsExpandedChangedStatic( DependencyObject sender, DependencyPropertyChangedEventArgs e ) {
			sender.CoerceValue( SelectedIndexProperty );
		}
		#endregion

		#region DP: SelectedIndex
		private static object SelectedIndexCoerceStatic( DependencyObject sender, object baseValue ) {
			if (GetIsExpanded( sender )) {
				ExpandableTabControl tc = sender as ExpandableTabControl;
				return Math.Max( 0, Math.Min( tc.Items.Count, (int)baseValue ) );
			} else return -1;
		}
		private static void SelectedIndexChangedStatic( DependencyObject sender, DependencyPropertyChangedEventArgs e ) { }
		#endregion

		#region DP: ContentWidth
		public static readonly DependencyProperty ContentWidthProperty = DependencyProperty.RegisterAttached( "ContentWidth", typeof( GridLength ), typeof( ExpandableTabControl ), new FrameworkPropertyMetadata( GridLength.Auto, FrameworkPropertyMetadataOptions.None, ContentWidthChangedStatic ) );
		public GridLength ContentWidth {
			get { return (GridLength)GetValue( ContentWidthProperty ); }
			set { SetValue( ContentWidthProperty, value ); }
		}
		public static GridLength GetContentWidth( DependencyObject element ) { return (GridLength)element.GetValue( ContentWidthProperty ); }
		public static void SetContentWidth( DependencyObject element, GridLength value ) { element.SetValue( ContentWidthProperty, value ); }
		private static void ContentWidthChangedStatic( DependencyObject sender, DependencyPropertyChangedEventArgs e ) { }
		#endregion

		static ExpandableTabControl() {
			SelectedIndexProperty.OverrideMetadata( typeof( ExpandableTabControl ), new FrameworkPropertyMetadata( 0, FrameworkPropertyMetadataOptions.None, SelectedIndexChangedStatic, SelectedIndexCoerceStatic ) );
		}
		public ExpandableTabControl() {
			CommandBindings.Add( new CommandBinding( ApplicationCommands.Close, TabClose_Executed ) );
		}
		protected override void OnSelectionChanged( SelectionChangedEventArgs e ) {
			if (e.AddedItems != null && e.AddedItems.Count > 0) {
				IsExpanded = true;
			}
			base.OnSelectionChanged( e );
		}
		protected override void OnItemsChanged( System.Collections.Specialized.NotifyCollectionChangedEventArgs e ) {
			base.OnItemsChanged( e );
			if (Items.Count == 0)
				Visibility = Visibility.Collapsed;
			else
				Visibility = Visibility.Visible;
		}
		protected virtual void TabClose_Executed( object sender, ExecutedRoutedEventArgs e ) {
			TabItem ti = e.Source as TabItem;
			Items.Remove( ti );
		}
	}
}